// pages/settings/buyermanagement/buyermanagement.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    namelist: {},

    inputShowed: false,
    inputVal: "",


    result:{},
    noResult:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },


  getname: function() {
    
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        wx.request({
          url: 'http://192.168.3.2/cloudwarehouse/settings/buyer/buyermanagement/getname.php',
          data: {
            openid: res.data
          },
          success(res) {
            console.log(res.data)
            that.setData({namelist : res.data})
            wx.hideLoading()
            if (res.data == '') {
              wx.showToast({
                title: '貌似还没人',
                image: '/icons/ques.png'
              })
              return;
            }
          },
        })

      },
    })
  },


  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },

  inputTyping: function (e) {
    var that = this
    if (that.search(this.data.namelist, e.detail.value)!= ''){
    this.setData({
      result : that.search(that.data.namelist, e.detail.value),
      noResult: true
    })
    }
    else
      this.setData({
        noResult: false
      });
    this.setData({
      inputVal: e.detail.value
    });
  },


  search: function (list, key) {
    var len = list.length;
    var arr = [];
    var reg = new RegExp(key);
    for (var i = 0; i < len; i++) {
      //如果字符串中不包含目标字符会返回-1
      if (list[i].buyerName.toLowerCase().match(key)) {
        arr.push(list[i]);
      }
    }
    this.setData({
      result : arr
    })
    return arr;
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    wx.showLoading({
      title: '加载中',
    })
    this.getname();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getname()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})